package com.michaelbulava.TFS.ui.controls.connect;

import com.intellij.openapi.project.Project;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.configuration.ProjectPreferences;
import com.michaelbulava.TFS.ui.dialogs.connect.ServerListDialog;
import com.microsoft.tfs.core.TFSConnection;
import com.microsoft.tfs.core.util.serverlist.ServerListConfigurationEntry;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;

/**
 * Created by mbulava on 6/19/2015.
 */
public class ServerSelectControl extends JPanel {

    private URI perferredServerURI;
    private ServerListConfigurationEntry lastAddedServerListEntry;
    private TFSConnection lastAddedConnection;

    public interface SelectionChangedListener{
        void onSelectionChanged(ServerListConfigurationEntry selectedConfigurationEntry);
    }

    //private ServerListConfigurationEntry lastAddedServerListEntry;
    //private TFSConnection lastAddedConnection;

    private ServerListCombo combo;
    private JButton serversButton;
    private boolean internalChange = false;
    private SelectionChangedListener changedListener;
    private Project myProject;

    public ServerSelectControl(){
        super();
        setLayout(new GridLayoutManager(2, 2, new Insets(4, 4, 4, 4), -1, -1));
        combo = new ServerListCombo();
        add(combo, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
                GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK |
                GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK |
                GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        serversButton = new JButton();
        serversButton.setText(Messages.getString("ServerSelectControl.ServersButtonText"));
        serversButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onServersButtonSelected();
            }
        });
//        this.$$$loadButtonText$$$(serversButton, ResourceBundle.getBundle("com/MichaelBulava/tfs/vcsintegration/core/Messages")
//                .getString("ServerSelectControl.ServersButtonText"));
        add(serversButton, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER,
                GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK |
                GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer();
        add(spacer1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
                GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));

        combo.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                ServerListConfigurationEntry entry = combo.getSelectedServerListEntry();
                if (entry != null)
                    perferredServerURI = combo.getSelectedServerListEntry().getURI();
                else
                    perferredServerURI = null;
                if (changedListener != null && !internalChange)
                    changedListener.onSelectionChanged(combo.getSelectedServerListEntry());
            }
        });

        populaterServersCombo(false);
    }

    public void setProject(Project project){
        myProject = project;
    }

    public void setServerURI(URI serverURI){
        this.perferredServerURI = serverURI;
        populaterServersCombo(serverURI != null);
    }

    public URI getServerURI(){
        ServerListConfigurationEntry serverListConfigurationEntry = combo.getSelectedServerListEntry();
        return serverListConfigurationEntry == null ? null : serverListConfigurationEntry.getURI();
    }

    @Override
    public void setEnabled(boolean enabled){
        super.setEnabled(enabled);
        combo.setEnabled(enabled);
        serversButton.setEnabled(enabled);
    }

    @Override
    public boolean isEnabled(){
        return super.isEnabled();
    }

    public boolean getEnabled(){
        return super.isEnabled();
    }

    protected void onServersButtonSelected(){
        ServerListConfigurationEntry lastSelectedServerListEntry = this.combo.getSelectedServerListEntry();
        ServerListDialog dialog = new ServerListDialog(this);
        //if (dialog.showAndGet()){

        //}
        dialog.show();

        populaterServersCombo(false);
        this.lastAddedServerListEntry = dialog.getLastAddedServerListEntry();
        this.lastAddedConnection = dialog.getLastAddedConnection();
        if (this.lastAddedServerListEntry != null){
            this.internalChange = true;
            try{
                this.combo.setSelectedServerListEntry(lastSelectedServerListEntry);
            } finally {
                this.internalChange = false;
            }
        } else if (lastSelectedServerListEntry != null && combo.contains(lastSelectedServerListEntry)){
            this.internalChange = true;
            try{
                combo.setSelectedServerListEntry(lastSelectedServerListEntry);
            } finally{
                internalChange = false;
            }
        } else {
            combo.setSelectedServerListEntry((ServerListConfigurationEntry)null);
        }
    }

    protected void populaterServersCombo(boolean select){
        combo.refresh();
        if (!select)
            return;
        URI selectedServerURI = null;
        if (this.perferredServerURI != null){
            selectedServerURI = this.perferredServerURI;
        } else {
            selectedServerURI = null;
            if (myProject != null)
                selectedServerURI = ProjectPreferences.getProjectInstance(myProject).getProjectServerURI();
            if (selectedServerURI != null && !this.combo.contains(selectedServerURI))
                selectedServerURI = null;
        }
        if (selectedServerURI != null) {
            combo.setSelectedServerListEntry(selectedServerURI);
            return;
        }
//        if (combo.getModel().getSize() == 2)
//            combo.selectFirstItem();
    }

    public final void setSelectionChangedListener(SelectionChangedListener listener){
        this.changedListener = listener;
    }

    private void onServersButton_Clicked(){
        ServerListConfigurationEntry lastSelectedServerListEntry = this.combo.getSelectedServerListEntry();

        ServerListDialog dialog = new ServerListDialog(this);
        dialog.show();
        if (dialog.isOK()){
            combo.refresh();

            TFSConnection lastAddedConnection = dialog.getLastAddedConnection();
            ServerListConfigurationEntry lastAddedServerListEntry = dialog.getLastAddedServerListEntry();
            this.internalChange = true;

            if (lastAddedServerListEntry != null){
                combo.setSelectedItem(lastAddedServerListEntry);
            } else {
                if (combo.contains(lastSelectedServerListEntry)){
                    combo.setSelectedServerListEntry(lastSelectedServerListEntry);
                } else {
                    combo.setSelectedServerListEntry((ServerListConfigurationEntry)null);
                }
            }
        }
    }


}


